import { CommonCompletionItem, ConstantCompletionMeta } from './types';
import { languageCase } from './languageCase';

export class FuncCompletionMeta {
  constructor(name, args, doc) {
    this.name = name;
    this.args = args;
    this.doc = doc;
  }

  toCompletionItem(currentWord, position) {
    const item = CommonCompletionItem.var(this.name, this.args, currentWord, position);
    item.documentation = this.doc;
    item.detail = this.args;
    return item;
  }

  getLabel() {
    return this.name;
  }

  getDetail() {
    return this.doc;
  }
}

export class FuncMap {
  all() {
    return [...this.appstack()];
  }
  CompletionItems(items) {
    const showItems = [];
    items.forEach((i) => {
      let tab;
      switch (i) {
        case 'mine':
          tab = '库成员';
          break;
        case 'repo':
          tab = '指定库名或路径';
          break;
        case 'file':
          tab = '指定文件路径';
          break;
        case 'lang':
          tab = '指定编程语言';
          break;
        case 'ext':
          tab = '指定文件扩展名';
          break;
        case 'author':
          tab = '指定作者';
          break;
        case 'reviewer':
          tab = '指定合并请求状态';
          break;
        default:
          tab = '指定可见性';
          break;
      }
      showItems.push(this.v(i, `${i }:`, tab));
    });
    return [...showItems];
  }
  appstack() {
    return [
      // this.f(
      //   'containers',
      //   'containers \n- name: main\n  image: {{ .Appstack.image. }}',
      //   'containers',
      // ),
      // this.f('namespace', '{{ .Values.namespace }}', 'namespace placeholder'),
    ];
  }
  lang(filterKey) {
    let showItems = [];
    languageCase
      .filter((i) => i.toLowerCase().startsWith(filterKey.toLowerCase()))
      .forEach((j) => {
        showItems.push(this.v(j, j, 'Tab'));
      });
    if (showItems.length > 6) {
      showItems = showItems.slice(0, 6);
    }
    return [...showItems];
  }

  mine() {
    return [this.v('true', 'true', 'Tab'), this.v('false', 'false', 'Tab')];
  }
  mrstate() {
    return [
      this.v('open', 'open', 'Tab'),
      this.v('closed', 'closed', 'Tab'),
      this.v('accepted', 'accepted', 'Tab'),
      this.v('merged', 'merged', 'Tab'),
      this.v('locked', 'locked', 'Tab'),
    ];
  }
  visibility() {
    return [
      this.v('private', 'private', 'Tab'),
      this.v('internal_pubilic', 'internal_pubilic', 'Tab'),
    ];
  }

  releaseVals() {
    return [
      this.v('Name', 'Name', 'Name of the release'),
      this.v('Time', 'Time', 'Time of the release'),
      this.v('Namespace', 'Namespace', 'Default namespace of the release'),
      this.v(
        'ServiceName',
        'Service',
        'Name of the service that produced the release (almost always Tiller)',
      ),
      this.v('IsUpgrade', 'IsUpgrade', 'True if this is an upgrade operation'),
      this.v('IsInstall', 'IsInstall', 'True if this is an install operation'),
      this.v('Revision', 'Revision', 'Release revision number (starts at 1)'),
    ];
  }

  filesVals() {
    return [
      this.f('Get', 'Get $path', 'Get file contents. Path is relative to chart.'),
      this.f(
        'GetBytes',
        'GetBytes $path',
        'Get file contents as a byte array. Path is relative to chart.',
      ),
    ];
  }

  capabilitiesVals() {
    return [
      this.v('KubeVersion', 'KubeVersion', 'Kubernetes version'),
      this.v('TillerVersion', 'TillerVersion', 'Tiller version'),
      this.f(
        'ApiVersions.Has',
        'ApiVersions.Has "batch/v1"',
        'Returns true if the given Kubernetes API/version is present on the cluster',
      ),
    ];
  }

  chartVals() {
    return [
      this.v('Name', '.Chart.Name', 'Name of the chart'),
      this.v('Version', '.Chart.Version', 'Version of the chart'),
      this.v('Description', '.Chart.Description', 'Chart description'),
      this.v('Keywords', '.Chart.Keywords', 'A list of keywords (as strings)'),
      this.v('Home', '.Chart.Home', 'The chart homepage URL'),
      this.v('Sources', '.Chart.Sources', 'A list of chart download URLs'),
      this.v('Maintainers', '.Chart.Maintainers', 'list of maintainer objects'),
      this.v('Icon', '.Chart.Icon', "The URL to the chart's icon file"),
      this.v(
        'AppVersion',
        '.Chart.AppVersion',
        'The version of the main app contained in this chart',
      ),
      this.v('Deprecated', '.Chart.Deprecated', 'If true, this chart is no longer maintained'),
      this.v(
        'TillerVersion',
        '.Chart.TillerVersion',
        'The version (range) if Tiller that this chart can run on.',
      ),
    ];
  }

  f(name, args, doc) {
    return new FuncCompletionMeta(name, args, doc);
  }

  v(name, use, doc) {
    return new ConstantCompletionMeta(name, use, doc);
  }
}
